<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyJyProductListTable;
use App\Http\Models\Mysql\SsyProcessingListDetailsTable;
use App\Http\Models\Mysql\SsyProcessingListTable;
use App\Http\Repository\WmsRepository;
use Illuminate\Console\Command;

class pushRFid extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:pushRFid {--repeat=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '推送RFid到巨沃';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $repeat = $this->option('repeat') ?? 0;
        //首次推送
        if(empty($repeat)){
            $this->pushData();
        }else{
            //失败重推
            $this->pushRepeat();
        }
    }

    //首次推送
    public function pushData()
    {
        $field = ["id","processing_code","type_no","sku_code","brand_code"];
        $processingList = (new SsyProcessingListTable())->where('is_oms', 1)->where('is_jw', 0)->get($field)->toArray();
        $subject = array_column(config('scm.subject'), 'brand_code', 'brand_code_s');
        foreach ($processingList as $item){
            //判断是否同步商品信息
            $hasJyProductObj = (new SsyJyProductListTable())->where('product_code', $item['sku_code'])->exists();
            if($hasJyProductObj){
                $processingDetailList = (new SsyProcessingListDetailsTable())->where('processing_code', $item['processing_code'])->where('type_no', $item['type_no'])
                    ->where('sku_code', $item['sku_code'])->get(["id", "number"])->toArray();
                foreach ($processingDetailList as $dItem){
                    $data = [
                        'items' => [
                            [
                                "goodsOwner" => $subject[$item['brand_code']],
                                "qty" => "1",
                                "sku" => $item['sku_code'],
                                "idCode" => $dItem['number'],
                                "idCodeType"=>""
                            ]
                        ]
                    ];
                    $res = WmsRepository::pushIFid($data);
                    (new SsyProcessingListDetailsTable())->updateData($dItem['id'], ['is_jw' => $res['success'] ? 1 : 2]);
                }
                if(!empty($processingDetailList)){
                    (new SsyProcessingListTable())->updateData($item['id'], ['is_jw' => 1]);
                }
            }
        }
    }

    //失败RFid重复推送
    public function pushRepeat()
    {
        $subject = array_column(config('scm.subject'), 'brand_code', 'brand_code_s');
        $processingDetailList = (new SsyProcessingListDetailsTable())->where('is_jw', 2)->whereIn('status', [1, 2, 3, 4, 5])->get(['id', 'sku_code', 'number'])->toArray();
        foreach ($processingDetailList as $item){
            //判断是否同步商品信息
            $hasJyProductObj = (new SsyJyProductListTable())->where('product_code', $item['sku_code'])->exists();
            if($hasJyProductObj){
                $processingObj = (new SsyProcessingListTable())->where('sku_code', $item['sku_code'])->first(['brand_code'])->toArray();
                $data = [
                    'items' => [
                        [
                            "goodsOwner" => $subject[$processingObj['brand_code']],
                            "qty" => "1",
                            "sku" => $item['sku_code'],
                            "idCode" => $item['number'],
                            "idCodeType"=>""
                        ]
                    ]
                ];
                $res = WmsRepository::pushIFid($data);
                (new SsyProcessingListDetailsTable())->updateData($item['id'], ['is_jw' => $res['success'] ? 1 : 22]);
            }else{
                (new SsyProcessingListDetailsTable())->updateData($item['id'], ['is_jw' => 3]);
            }
        }
    }
}
